/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.entity.Express;
import com.thinkgem.jeesite.modules.shop.entity.ExpressTools;
import com.thinkgem.jeesite.modules.shop.service.ExpressService;
import com.thinkgem.jeesite.modules.shop.service.ExpressToolsService;

/**
 * 快递工具Controller
 * @author xxs
 * @version 2016-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/expressTools")
public class ExpressToolsController extends BaseController {

	@Autowired
	private ExpressToolsService expressToolsService;
	@Autowired
	private ExpressService expressService;
	
	@ModelAttribute
	public ExpressTools get(@RequestParam(required=false) String id) {
		ExpressTools entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expressToolsService.get(id);
		}
		if (entity == null){
			entity = new ExpressTools();
		}
		return entity;
	}
	
	@RequiresPermissions("shop:expressTools:view")
	@RequestMapping(value = {"list", ""})
	public String list(ExpressTools expressTools, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpressTools> page = expressToolsService.findPage(new Page<ExpressTools>(request, response), expressTools); 
		model.addAttribute("page", page);
		return "modules/shop/expressToolsList";
	}

	@RequiresPermissions("shop:expressTools:view")
	@RequestMapping(value = "form")
	public String form(ExpressTools expressTools, Model model) {
		model.addAttribute("expressTools", expressTools);
		model.addAttribute("expressList", expressService.findList(new Express()));
		return "modules/shop/expressToolsForm";
	}

	@RequiresPermissions("shop:expressTools:edit")
	@RequestMapping(value = "save")
	public String save(ExpressTools expressTools, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, expressTools)){
			return form(expressTools, model);
		}
		expressToolsService.save(expressTools);
		addMessage(redirectAttributes, "保存快递工具成功");
		return "redirect:"+Global.getAdminPath()+"/shop/expressTools/?repage";
	}
	
	@RequiresPermissions("shop:expressTools:edit")
	@RequestMapping(value = "delete")
	public String delete(ExpressTools expressTools, RedirectAttributes redirectAttributes) {
		expressToolsService.delete(expressTools);
		addMessage(redirectAttributes, "删除快递工具成功");
		return "redirect:"+Global.getAdminPath()+"/shop/expressTools/?repage";
	}

}